package cn.roadtrip.mtravelbusinessmodule.bs.tuorgroup;

import cn.roadtrip.mtravelbusinessmodule.sql.couple.travelgroup.tables.records.BsTravelgroupBindkeyRecord;
import cn.roadtrip.utilmodule.util.DataBean;
import cn.roadtrip.utilmodule.util.enumpck.Enum_TravelGroupBindKeyStats;
import lombok.*;
import org.jooq.DSLContext;
import org.jooq.TableRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

import static cn.roadtrip.mtravelbusinessmodule.sql.couple.travelgroup.tables.BsTravelgroupBindkey.BS_TRAVELGROUP_BINDKEY;

/**
 * 旅游团关键字、标签操作
 */
@Component
public class TravelGroupBindKeyImpl {
    @Autowired
    private DSLContext dslContext;

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @EqualsAndHashCode
    public static class Model_pushHomePage {
        private String travelGroupId;
        private java.util.List<String> keyCodes;
    }

    /**
     * 小团绑定关键字并推荐到首页
     *
     * @param model
     * @return
     */
    public DataBean pushHomePage(Model_pushHomePage model) {
        try {
            return dslContext.transactionResult(ctx -> {
                dslContext.update(BS_TRAVELGROUP_BINDKEY)
                        .set(BS_TRAVELGROUP_BINDKEY.STATS, (Enum_TravelGroupBindKeyStats.Trash.getCode()))
                        .where(
                                BS_TRAVELGROUP_BINDKEY.STATS.in(
                                        Enum_TravelGroupBindKeyStats.Top.getCode(),
                                        Enum_TravelGroupBindKeyStats.Down.getCode()
                                )
                        )
                        .and(BS_TRAVELGROUP_BINDKEY.LOCK.eq(false))
                        .and(BS_TRAVELGROUP_BINDKEY.TRAVELGROUPID.eq(model.getTravelGroupId()))
                        .execute();
                java.util.List<TableRecord<BsTravelgroupBindkeyRecord>> tlist = new ArrayList<>();
                long t = System.currentTimeMillis();
                for (String k : model.getKeyCodes()) {
                    tlist.add(
                            new BsTravelgroupBindkeyRecord(
                                    Enum_TravelGroupBindKeyStats.Top.getCode(),
                                    false,
                                    model.getTravelGroupId(),
                                    k,
                                    t
                            )
                    );
                }
                dslContext.batchInsert(tlist);
                dslContext.commit();
                return DataBean.suc(true, 1, "suc", "操作成功");
            });
        } catch (Exception e) {
            e.printStackTrace();
            return DataBean.err("trans-err", e.getMessage());
        }
    }


    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @EqualsAndHashCode
    public static class Model_downFromPage {
        private String travelGroupId;
    }

    /**
     * 从首页下架
     *
     * @param model
     * @return
     */
    public DataBean downFromPage(Model_downFromPage model) {
        try {
            return dslContext.transactionResult(ctx -> {
                dslContext.update(BS_TRAVELGROUP_BINDKEY)
                        .set(BS_TRAVELGROUP_BINDKEY.STATS, (Enum_TravelGroupBindKeyStats.Trash.getCode()))
                        .set(BS_TRAVELGROUP_BINDKEY.MODIFYTIME, System.currentTimeMillis())
                        .where(
                                BS_TRAVELGROUP_BINDKEY.STATS.in(
                                        Enum_TravelGroupBindKeyStats.Top.getCode(),
                                        Enum_TravelGroupBindKeyStats.Down.getCode()
                                )
                        )
                        .and(BS_TRAVELGROUP_BINDKEY.LOCK.eq(false))
                        .and(BS_TRAVELGROUP_BINDKEY.TRAVELGROUPID.eq(model.getTravelGroupId()))
                        .execute();
                dslContext.commit();
                return DataBean.suc(true, 1, "suc", "操作成功");
            });
        } catch (Exception e) {
            e.printStackTrace();
            return DataBean.err("trans-err", e.getMessage());
        }
    }


}
